define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'workorder/fields/index' + location.search,
                    add_url: 'workorder/fields/add',
                    edit_url: 'workorder/fields/edit',
                    del_url: 'workorder/fields/del',
                    multi_url: 'workorder/fields/multi',
                    import_url: 'workorder/fields/import',
                    table: 'workorder_fields',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'position',
                            title: __('Position'),
                            searchList: {"0": __('Position 0'), "1": __('Position 1'), "2": __('Position 2')},
                            formatter: Table.api.formatter.status
                        },
                        {field: 'name', title: __('Name')},
                        {field: 'title', title: __('Title')},
                        {
                            field: 'type_list',
                            title: __('Type_list'),
                            searchList: {
                                "string": __('Type_list string'),
                                "textarea": __('Type_list textarea'),
                                "text": __('Type_list text'),
                                "editor": __('Type_list editor'),
                                "number": __('Type_list number'),
                                "date": __('Type_list date'),
                                "time": __('Type_list time'),
                                "datetime": __('Type_list datetime'),
                                "select": __('Type_list select'),
                                "selects": __('Type_list selects'),
                                "image": __('Type_list image'),
                                "images": __('Type_list images'),
                                "file": __('Type_list file'),
                                "files": __('Type_list files'),
                                "switch": __('Type_list switch'),
                                "checkbox": __('Type_list checkbox'),
                                "radio": __('Type_list radio'),
                                "city": __('Type_list city')
                            },
                            formatter: Table.api.formatter.normal
                        },
                        // {field: 'default', title: __('Default')},
                        // {field: 'rule', title: __('Rule')},
                        // {field: 'errormsg', title: __('Errormsg')},
                        {field: 'notice', title: __('Notice')},
                        {field: 'field_length', title: __('Field_length')},
                        {
                            field: 'isfilter',
                            title: __('Isfilter'),
                            searchList: {"1": __('Yes'), "0": __('No')},
                            formatter: Controller.api.formatter.toggle
                        },
                        // {field: 'extend', title: __('Extend')},
                        {
                            field: 'isbasicinfo',
                            title: __('Isbasicinfo'),
                            searchList: {"0": __('Isbasicinfo 0'), "1": __('Isbasicinfo 1')},
                            formatter: Controller.api.formatter.status
                        },
                        {
                            field: 'autofill',
                            title: __('Autofill'),
                            searchList: {"0": __('Autofill 0'), "1": __('Autofill 1')},
                            formatter: Controller.api.formatter.status
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"0": __('Status 0'), "1": __('Status 1')},
                            formatter: Table.api.formatter.status
                        },
                        {field: 'weigh', title: __('Weigh')},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {

                                var ban_del = ['title', 'describe'];
                                if (ban_del.includes(row.name)) {
                                    var that = $.extend({}, this);
                                    var table = $(that.table).clone(true);
                                    $(table).data("operate-del", null);
                                    that.table = table;
                                    return Table.api.formatter.operate.call(that, value, row, index);
                                }

                                return Table.api.formatter.operate.call(this, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $("form#add-form").data("validator-options", {ignore: ':hidden'});
                $(document).on("change", "#c-type_list", function () {
                    let typeList = $(this).val();
                    $(".tf").addClass("hidden");
                    $(".tf.tf-" + typeList).removeClass("hidden");

                    if (typeList == 'switch') {
                        let defaultEl = $('#c-default');
                        defaultEl.val(defaultEl.val() ? defaultEl.val():'0')
                    }
                });

                $(document).on("change", '#c-position', function () {
                    $(".pp").addClass("hidden");
                    $(".pp.position-" + $(this).val()).removeClass("hidden");
                })

                Form.api.bindevent($("form[role=form]"));
                $("#c-type_list").trigger("change");
                $("#c-position").trigger("change");
            },
            formatter: {
                toggle: function (value, row, index) {
                    if (row.position == 0) {
                        var that = $.extend({}, this);
                        var table = $(that.table).clone(true);
                        that.table = table;
                        return Table.api.formatter.toggle.call(that, value, row, index);
                    } else {
                        return '-';
                    }
                },
                status: function (value, row, index) {
                    if (row.position == 0) {
                        var that = $.extend({}, this);
                        var table = $(that.table).clone(true);
                        that.table = table;
                        return Table.api.formatter.status.call(that, value, row, index);
                    } else {
                        return '-';
                    }
                }
            }
        }
    };
    return Controller;
});